package com.imooc.bilibili.api;

import com.imooc.bilibili.domain.core.JsonResponse;
import com.imooc.bilibili.domain.video.Danmu;
import com.imooc.bilibili.support.UserSupport;
import com.imooc.bilibili.video.IDanmuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;

/**
 * 弹幕Api
 *
 * @author gcq
 * @Create 2022-06-08
 */
@RestController
public class DanMuApi {

    @Autowired
    private IDanmuService danmuService;

    @Autowired
    private UserSupport userSupport;

    /**
     * 获取当前视频弹幕信息
     *
     * @param videoId
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @GetMapping("/danmus")
    public JsonResponse<List<Danmu>> getDanMus(@RequestParam Long videoId,
                                               String startTime,
                                               String endTime) throws ParseException {
        List<Danmu> list;
        // 区分用户登录和未登录
        // 用户登录可以按照时间查询弹幕
        // 用户未登录不可以按照时间进行查询
        try {
            userSupport.getCurrentUserId();
            list = danmuService.getDanMus(videoId, startTime, endTime);
        } catch (Exception e) {
            list = danmuService.getDanMus(videoId, null, null);
        }
        return new JsonResponse<>(list);
    }
}